# -*- coding: utf-8 -*-
"""
******* 文档说明 ******
远程操作 云电脑操作端后台代码

# 当前项目: OriginCMD
# 创建时间: 2021-03-20  22:50
# 开发作者: Vincent
# 版    本: V1.0
"""
import flask
from flask import request, Flask

app = Flask('app')

print('Flask Version:{} '.format(flask.__version__))

server_res = 'None'  # 返回结果
server_cmd = ''  # 下行命令


# 服务器轮询操作
@app.route('/op', methods=['POST', 'GET'])
def op():
    global server_cmd
    global server_res

    # 命令传回结果
    server_res = request.data.decode('utf-8')

    # 为非None说明已返回结果，命令清空
    if server_res != 'None':
        print('RES: ', server_res)
        server_cmd = ''

    return server_cmd


# 发送命令
@app.route('/cmd', methods=['POST', 'GET'])
def cmd():
    global server_cmd
    global server_res

    server_res = 'None'

    # 命令传回结果
    server_cmd = request.data.decode('utf-8')
    print('CMD:', server_cmd)
    return server_cmd


# 读取结果
@app.route('/res', methods=['POST', 'GET'])
def res():
    global server_res

    return server_res


# 启动API服务
app.run(host='0.0.0.0', port=8899, debug=False)
